package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dbConnection.Database;

public class AdminUserSearchPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Database DB;
	public MainScreen app;

	public AdminUserSearchPanel(Database dB2, final MainScreen app) {
		this.DB = dB2;
		this.app = app;

		// panel for searching by name, id, etc
		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.X_AXIS)); // 2
																				// rows,
																				// adjustable
																				// columns
		final JTextField userName = new JTextField();
		final JTextField userID = new JTextField();
		final JTextField email = new JTextField();
		JButton search = new JButton("Search");

		searchPanel.add(new JLabel("Username"));
		searchPanel.add(userName);
		searchPanel.add(new JLabel("User ID"));
		searchPanel.add(userID);
		searchPanel.add(new JLabel("email"));
		searchPanel.add(email);
		searchPanel.add(search);

		final JPanel searchResults = new JPanel();
		//final JScrollPane resultsPanel = new JScrollPane(searchResults);
		searchResults.setLayout(new BoxLayout(searchResults,BoxLayout.Y_AXIS));

		EnterListener searchListener = new EnterListener(search);
		userName.addActionListener(searchListener);
		userID.addActionListener(searchListener);
		email.addActionListener(searchListener);
		
		// search function
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchResults.removeAll();

				Connection connection = DB.con;
				String query = "SELECT Name,UserId,Email FROM user WHERE UserId LIKE '%"
						+ userID.getText()
						+ "%' AND Name LIKE '%"
						+ userName.getText()
						+ "%' AND Email LIKE '%"
						+ email.getText() + "%';";
//				System.out.println(query);
				PreparedStatement ps;
				try {
					ps = connection.prepareStatement(query);
					ResultSet rs = ps.executeQuery();
					while (rs.next()) {
						int uID = Integer.parseInt(rs.getString("UserID"));
						searchResults.add(new AdminUserSearchResult(DB, uID));
//						System.out.print("Found userID " + uID);

					}

				} catch (SQLException e1) {
					e1.printStackTrace();
				}

				searchResults.revalidate();
			}
		});

		JPanel adminButtons = new JPanel();
		adminButtons.setLayout(new BoxLayout(adminButtons, BoxLayout.X_AXIS));

		JButton assessFines = new JButton("Inflict Fines");
		final JButton viewFines = new JButton("View Fines");
		JButton logout = new JButton("logout");

		assessFines.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchResults.removeAll();

				
				Connection connection = DB.con;
				// "UPDATE user SET fines = 0.1 +user.fines "
						//+ "WHERE user.UserId ="
						String query ="SELECT inventory.UserId FROM inventory where "
						+ "curdate() > DueDate  ";
//						System.out.println(query);
				// String query =
				// "SELECT UserId FROM inventory WHERE UserId LIKE '%"
				// + userID.getText() + "%' AND Fines <> 0 ";
//				System.out.println(query);
				PreparedStatement ps;
				try {
					ps = connection.prepareStatement(query);
					ResultSet rs = ps.executeQuery();
					 while (rs.next()) {
					 int inID = Integer.parseInt(rs.getString("inventory.UserId"));
					 String update = "UPDATE user SET fines = 0.1 +user.fines "
								+ "WHERE user.UserId ="+inID+"";
//					 System.out.println(update);
					 PreparedStatement fineStatement = connection.prepareStatement(update);
					 fineStatement.executeUpdate();
					//
					// resultsPanel.add(new AdminUserSearchResult(DB, uID));
					// System.out.print("Found userID " + uID);
					//
					 }

				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				viewFines.doClick();
				//searchResults.revalidate();
			}
		});

		viewFines.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchResults.removeAll();
				Connection connection = DB.con;
				String query = "SELECT UserId FROM user WHERE UserId LIKE '%"
						+ userID.getText() + "%' AND Fines <> 0 ";
//				System.out.println(query);
				PreparedStatement ps;
				try {
					ps = connection.prepareStatement(query);
					ResultSet rs = ps.executeQuery();
					while (rs.next()) {
						int uID = Integer.parseInt(rs.getString("UserID"));

						searchResults.add(new AdminUserSearchResult(DB, uID));
//						System.out.print("Found userID " + uID);

					}

				} catch (SQLException e1) {
					e1.printStackTrace();
				}

				searchResults.revalidate();
			}
		});

		logout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				app.showLogin();
			}
		});

		adminButtons.add(viewFines);
		adminButtons.add(assessFines);
		adminButtons.add(logout);

		setLayout(new BorderLayout());
		add(searchPanel, BorderLayout.NORTH);
		add(searchResults,BorderLayout.CENTER);
		//add(resultsPanel, BorderLayout.CENTER);
		add(adminButtons, BorderLayout.SOUTH);

	}

}
